/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.listeners;

import com.minecraft.moonlake.api.event.MoonLakeListener;
import com.minecraft.moonlake.legendaryweaponrel.LegendaryWeaponRelPlugin;
import com.minecraft.moonlake.legendaryweaponrel.weapons.*;
import com.minecraft.moonlake.legendaryweaponrel.weapons.tool.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;

public class EntityDamageListener implements MoonLakeListener {

    private final LegendaryWeaponRelPlugin main;

    public EntityDamageListener(LegendaryWeaponRelPlugin main) {
        this.main = main;
    }

    public LegendaryWeaponRelPlugin getMain() {
        return main;
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onDamage(EntityDamageByEntityEvent event) {
        // 处理实体受伤是实体所为事件
        Entity damager = event.getDamager();
        Entity entity = event.getEntity();

        if(!(damager instanceof Player)) return; // 攻击者不为玩家则返回
        if(!(entity instanceof LivingEntity)) return; // 受伤者不为活着实体则返回

        Player damagePlayer = (Player) damager;
        ItemStack handItem = damagePlayer.getItemInHand();

        // 处理 Agni
        Agni agni = Weapons.getWeapon(Agni.class, damagePlayer, handItem);
        if(agni != null) agni.onConsumer(damagePlayer).accept(event);
        // 处理 Gamble
        Gamble gamble = Weapons.getWeapon(Gamble.class, damagePlayer, handItem);
        if(gamble != null) gamble.onConsumer(damagePlayer).accept(event);
        // 处理 OverKill
        OverKill overKill = Weapons.getWeapon(OverKill.class, damagePlayer, handItem);
        if(overKill != null) overKill.onConsumer(damagePlayer).accept(event);
        // 处理 Simon
        Simon simon = Weapons.getWeapon(Simon.class, damagePlayer, handItem);
        if(simon != null) simon.onConsumer(damagePlayer).accept(event);
        // 处理 Storm
        Storm storm = Weapons.getWeapon(Storm.class, damagePlayer, handItem);
        if(storm != null) storm.onConsumer(damagePlayer).accept(event);
        // 处理 Vampyr
        Vampyr vampyr = Weapons.getWeapon(Vampyr.class, damagePlayer, handItem);
        if(vampyr != null) vampyr.onConsumer(damagePlayer).accept(event);
    }
}
